package com.dfcj.oauth2server.configure;

import com.dfcj.oauth2server.service.MyUserDetailsService;
import io.lettuce.core.support.RedisClientFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

@Configuration
@EnableAuthorizationServer
public class OAuth2Configure extends AuthorizationServerConfigurerAdapter {
    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

//    @Autowired
//    private TokenStore tokenStore;
//
//    @Autowired
//    private UserApprovalHandler approvalHandler;

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Autowired
    private MyUserDetailsService myUserDetailsService;

    @Bean
    public MyRedisTokenStore tokenStore(){
        return new MyRedisTokenStore(redisConnectionFactory);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
        clients.inMemory()
                .withClient("user-service")
                .secret("{noop}123456")
                .scopes("service")
                .authorizedGrantTypes("refresh_token","password","client_credentials")
                .accessTokenValiditySeconds(60)
                .refreshTokenValiditySeconds(6000)
        .and()
                .withClient("client-service")
                .secret("{noop}123456")
                .scopes("client")
                .authorizedGrantTypes("refresh_token","client_credentials")
                .accessTokenValiditySeconds(60)
                .refreshTokenValiditySeconds(6000);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception{
        endpoints.authenticationManager(authenticationManager).userDetailsService(myUserDetailsService).tokenStore(tokenStore());
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .allowFormAuthenticationForClients()
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("permitAll()");
//                .checkTokenAccess("isAuthenticated()");
    }



//    @Override
//    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception{
////        endpoints.tokenStore(tokenStore()).userApprovalHandler(userApprovalHandler(tokenStore())).authenticationManager(authenticationManager);
//        endpoints.tokenStore(tokenStore).userApprovalHandler(approvalHandler)
//                .authenticationManager(authenticationManager);
//    }


}
